package com.gj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gj.reggie.common.BaseContext;
import com.gj.reggie.entity.R;
import com.gj.reggie.entity.ShoppingCart;
import com.gj.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 郭嘉
 * @date 2023/1/7 - 11:54
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCard(@RequestBody ShoppingCart shoppingCart){

        // 设置用户id，指定当前是哪个用户的购物车数据
        Long currentId=BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        // 查询当前菜品或者套餐是否存在，若存在，在number项上加一，不存在，则添加到购物车，number为1
        Long dishId=shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId!=null){
            //添加的为菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //否则为套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);

        if(cartServiceOne!=null){
            //已存在
            cartServiceOne.setNumber(cartServiceOne.getNumber()+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //不存在
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getCardList(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 减少购物车数量
     * @param cart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart cart){
        ShoppingCart shoppingCart=new ShoppingCart();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        if(cart.getDishId()!=null){
            //菜品
            queryWrapper.eq(ShoppingCart::getDishId,cart.getDishId());
            shoppingCart=shoppingCartService.getOne(queryWrapper);
            if(shoppingCart.getNumber()==1){
            LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            shoppingCartService.remove(lambdaQueryWrapper);
            shoppingCart.setNumber(0);
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartService.updateById(shoppingCart);
            }
        }else {
            //套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,cart.getSetmealId());
            shoppingCart=shoppingCartService.getOne(queryWrapper);
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            if(shoppingCart.getNumber()==1){
                LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
                shoppingCartService.remove(lambdaQueryWrapper);
                shoppingCart.setNumber(0);
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartService.updateById(shoppingCart);
            }
        }

        return R.success(shoppingCart);
    }
}
